package ro.ubbcluj.cs.validation.framework.tags.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class ResourceUtils {

	private Logger log = LoggerFactory.getLogger(ResourceUtils.class);
	private Properties messages;
	
	public ResourceUtils() {
		this.messages = new Properties();
		try {
			InputStream msgFileIS = Thread.currentThread().getContextClassLoader().getResourceAsStream("messages.properties");
			messages.load(msgFileIS);
		} catch (IOException e) {
			log.error("Cannot load the resource message file: "+e.getMessage());
		}
	}
	
	public final String getMessage(String key) {
		return messages.getProperty(key, key);
	}
	
	public final String getMessage(String key, String[] attributes) {
		String msg = messages.getProperty(key, key);
		int idx = 0;
		for(String attr : attributes){
			msg = msg.replace("{"+idx+"}", attr);
			idx++;
		}
		return msg;
	}
}